package com.sxt.practiceapplication.activities.test;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;

import com.sxt.permissionannotation.PermissionDenied;
import com.sxt.permissionannotation.PermissionGrant;
import com.sxt.permissionannotation.PermissionRational;
import com.sxt.permissionhelperlib.PerMissionHelper;
import com.sxt.permissionhelperlib.PermissionRationalCallback;
import com.sxt.practiceapplication.R;
import com.sxt.practiceapplication.base.BaseActivity;
import com.sxt.practiceapplication.utils.ToastUtils;
import com.sxt.practiceapplication.utils.camera.CameraUtil;

public class Camera2TestActivity extends BaseActivity implements View.OnClickListener {

    private CameraUtil cameraUtil;
    private Button start;
    private Button change;
    private Button end;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera2_test);
        TextureView mTextureView = findViewById(R.id.tv_test);
        cameraUtil = new CameraUtil(this, mTextureView);
        start = findViewById(R.id.start);
        change = findViewById(R.id.change_facing);
        end = findViewById(R.id.end);
        start.setOnClickListener(this);
        change.setOnClickListener(this);
        end.setOnClickListener(this);
        PerMissionHelper.requestPermission(this, 100, Manifest.permission.CAMERA);
    }

    @Override
    protected void onResume() {
        super.onResume();
        cameraUtil.initSurfaceTexture();
//        cameraUtil.setPreviewCallback((mImageReader, handler) -> {
//            mImageReader.setOnImageAvailableListener(reader -> {
//                Image image = reader.acquireLatestImage();
//                if (image != null) {
//                    //我们可以将这帧数据转成字节数组，类似于Camera1的PreviewCallback回调的预览帧数据
////            ByteBuffer buffer = image.getPlanes()[0].getBuffer();
////            byte[] data = new byte[buffer.remaining()];
////            buffer.get(data);
//
//                    image.close();
//                }
//            }, handler);
//        });

    }

    @PermissionGrant(100)
    public void testPermission(String[] permissions) {
        ToastUtils.show("权限申请成功");
    }

    @PermissionDenied(100)
    public void testPermissionDenied(String[] permissions) {
        StringBuilder permissionDenied = new StringBuilder();
        for (String permission : permissions) {
            permissionDenied.append(permission).append("\n");
        }
        new AlertDialog.Builder(this)
                .setTitle("授权提示")
                .setMessage("以下权限被拒绝了,需要您手动授权: " + permissionDenied.toString())
                .setPositiveButton("好的", (dialog, which) -> {

                }).create().show();
    }

    @PermissionRational(100)
    public void testPermissionRational(String[] permissions, PermissionRationalCallback rationalCallback) {
        new AlertDialog.Builder(this)
                .setTitle("授权提示")
                .setMessage("请授予app运行时所必须的权限")
                .setPositiveButton("好的", (dialog, which) -> {
                    if (rationalCallback != null) {
                        rationalCallback.onRationalExecute();
                    }
                }).create().show();
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PerMissionHelper.onRequestPermissionResult(this, requestCode, permissions, grantResults);
    }

    @Override
    public void onClick(View v) {
        if (v == start) {
            cameraUtil.openCamera();
        } else if (v == end) {
            cameraUtil.closeCamera();
        } else if (v == change) {
            cameraUtil.switchCameraFacing();
        }
    }
}